package org.cyk.demo2;

import lombok.With;
import org.xml.sax.helpers.ParserAdapter;

import java.lang.reflect.AnnotatedArrayType;
import java.util.*;
import java.util.stream.Collectors;

public class Solution {

    private UserinfoMapper userinfoMapper = new UserinfoMapper();

    public void test1(Integer id) {
        //写法一: 原生
        Userinfo userinfo = userinfoMapper.getUserinfoById(id);
        if(userinfo != null) {
            userinfo.setAge(userinfo.getAge() + 1);
        }

        //写法二: 使用 Optional 函数式编程，一行搞定
        Optional.ofNullable(userinfoMapper.getUserinfoById(id)).ifPresent(p -> p.setAge(p.getAge() + 1));
    }

    public boolean isEmpty(Object obj) {
        if(obj instanceof String) {
            return obj != null && obj.equals("");
        }
        return obj != null;
    }

    public void test2() {
        Userinfo userinfo = new Userinfo(1, null, 20);
        //原生
        if(userinfo == null || isEmpty(userinfo.getUsername())
                || isEmpty(userinfo.getAge())) {
            throw new RuntimeException("用户数据不能为空!");
        }
        //用 Optional 改进
        Optional.ofNullable(userinfo)
                .filter(p -> isEmpty(p.getUsername()) || isEmpty(p.getAge()))
                .orElseThrow(() -> new RuntimeException("用户数据不能为空"));
    }

    public void test3() {
        Userinfo userinfo = new Userinfo();
        Optional.ofNullable(userinfo)
                .map(u -> {
                    //业务逻辑...
                    u.setId(2);
                    u.setUsername("cyk");
                    u.setAge(20);
                    return u;
                })
                .orElse(new Userinfo(100, "xxx", 0));
        System.out.println(userinfo);
    }

    public void test4() {
        Userinfo userinfo = new Userinfo(1, "cyk", 20);
        //if-else
        if(userinfo != null) {
            System.out.println("hello " + userinfo.getUsername());
        } else {
            System.out.println("world");
        }
        //ifPresentOrElse
        Optional.ofNullable(userinfo)
                .ifPresentOrElse(u -> System.out.println("hello " + u.getUsername()),
                        () -> System.out.println("world"));
    }

    public Userinfo test5() {
        Userinfo nullinfo = null;
        //原生
        if(nullinfo == null) {
            Userinfo userinfo1 = new Userinfo();
            userinfo1.setId(1);
            if(userinfo1 == null) {
                Userinfo userinfo2 = new Userinfo();
                userinfo2.setId(2);
                return userinfo2;
            }
            return userinfo1;
        }
        //Optional 改进后
        Optional<Userinfo> or = Optional.ofNullable(nullinfo)
                .or(() -> { // nullinfo 为 null，因此执行此 or
                    Userinfo userinfo1 = new Userinfo();
                    userinfo1.setId(1);
                    return Optional.ofNullable(null); //返回一个空的 Optional
                })
                .or(() -> { // 上一个 or 返回的 Optional 中没有值时，执行此 or
                    Userinfo userinfo2 = new Userinfo();
                    userinfo2.setId(2);
                    return Optional.ofNullable(userinfo2);
                });
        System.out.println(or); //输出: Optional[Userinfo(id=2, username=null, age=null)]

        return or.get();
    }

    public void test6() {

        //1.创建不可变的 List
        List<String> strList = List.of("aaa", "bbb", "ccc");
        //strList.add("ddd"); //报错: Exception in thread "main" java.lang.UnsupportedOperationException

        //2.创建一个不可变的 set/map
        Set<String> set = Set.of("aaa", "bbb", "ccc");
        Map<String, String> map1 = Map.of("1", "aaa", "2", "bbb", "3", "ccc");
        //以上 map 创建方式可能会导致分不清 key value，因此也可以通过以下方式创建
        Map<String, String> map2 = Map.ofEntries(Map.entry("1", "bbb"), Map.entry("2", "bbb"));
        //set/map 不能保证输出的顺序和定义 键值对 的顺序一致
        System.out.println(map1);
    }

    public void test7() {
        List<Integer> list = List.of(1, 2, 3, 4, 5, 6, 7);
        List<Integer> result = list.stream().takeWhile(val -> val < 5).toList();
        System.out.println(result); //输出: [1, 2, 3, 4]
    }

    public void test8() {
        List<Integer> list = List.of(1, 2, 3, 4, 5, 6, 7);
        List<Integer> result = list.stream().dropWhile(val -> val < 5).toList();
        System.out.println(result); //输出: [5, 6, 7]
        System.out.println(list); //Ps: 这里的删除，不影响 list 中原本的值
    }

    public void test9() {
        List<String> srcList = new ArrayList<>();
        srcList.add("aaa");
        srcList.add("bbb");
        List<String> copyList = List.copyOf(srcList);
        srcList.add("ccc");
        System.out.println(srcList); //[aaa, bbb, ccc]
        System.out.println(copyList);//[aaa, bbb]
    }

    public void test10() {
        //原始
        String before = "{\n" +
                        "     \"name\":\"cyk\",\n" +
                        "     \"age\": 20;\n" +
                        "}";
        //增强
        String after = """
                      {
                          "name": "cyk",
                          "age": 20
                      }
                      """;

        System.out.println(before);
        System.out.println(after);
    }

    public void test11() {
        List<String> list1 = List.of("aaa", "bbb");
        //可读可写
        List<String> list2 = list1.stream().toList();
        //可读，不可写
        List<String> list3 = list2.stream().collect(Collectors.toList());
        List<String> list4 = list1.stream().collect(Collectors.toUnmodifiableList());
    }

    public void test12() {
        Object val = "aaa";

        //before
        if(val instanceof String) {
            String newVal = (String) val;
            System.out.println(newVal);
        }
        //after
        if(val instanceof String newVal) {
            System.out.println(newVal);
        }
    }

    public void test13() {
//        //1.字符串匹配
//        String val1 = "aaa";
//
//        //before
//        switch (val1) {
//            case "abc":
//                System.out.println("abc!");
//                break;
//            case "aaa":
//                System.out.println("aaa!");
//                break;
//            default:
//                System.out.println("error!");
//        }
//        //after
//        switch (val1) {
//            case "abc" -> System.out.println("abc!");
//            case "aaa" -> System.out.println("aaa!");
//            default -> System.out.println("error");
//        }
//
//        //2.类型判断
//        Object val2 = "aaa";
//
//        //before
//        if(val2 instanceof String) {
//            String newVal = (String) val2;
//            System.out.println("String:" + newVal);
//        } else if(val2 instanceof Integer) {
//            Integer newVal = (Integer) val2;
//            System.out.println("Integer:" + newVal);
//        } else {
//            System.out.println(val2);
//        }
//
//        //after
//        switch (val2) {
//            case String str -> {
//                String newVal = str;
//                System.out.println("String:" + newVal);
//            }
//            case Integer num -> {
//                Integer newVal = num;
//                System.out.println("Integer:" + newVal);
//            }
//            default -> System.out.println(val2);
//        }
//
//        //3.对 null 的处理
//        String val3 = null;
//        switch (val3) {
//            case null -> System.out.println("null!");
//            case "aaa" -> System.out.println("aaa!");
//            case "bbb" -> System.out.println("bbb!");
//            default -> System.out.println("error");
//        }

        //4.对条件判断的处理
        Object obj = 1;
        switch (obj) {
            case String str -> {
                String newVal = str;
                System.out.println("String:" + newVal);
            }
            case Integer num && num < 3 -> {
                Integer newVal = num;
                System.out.println("Integer:" + newVal);
            }
            default -> System.out.println("error!");
        }

    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        solution.test13();
    }

}
